﻿using System.Collections.Generic;
using System.Linq;
using ComicQ.Domain.Table;
using NHibernate.Linq;
using Member = ComicQ.Domain.Table.Member;

namespace LoginService
{
    public class LoginService : ILoginService
    {
        private IList<Member> _members;
        private IList<Staff> _staves;

        public int CheckLogin(string username, string password)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                _members = session.Query<Member>().ToList();
            }

            foreach (var member in _members)
            {
                if (!member.Username.Equals(username)) continue;
                
                if (member.Password.Equals(password))
                {
                    return 1;
                }

                return -1;
            }

            return 0;
        }

        public int CheckLoginStaff(string username, string password)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                _staves = session.Query<Staff>().ToList();
            }

            foreach (var staff in _staves)
            {
                if (!staff.Username.Equals(username)) continue;

                if (staff.Password.Equals(password))
                {
                    return 1;
                }

                return -1;
            }

            return 0;
        }
    }
}
